package com.mixcus.server.service.impl;

import com.mixcus.server.dao.UserDao;
import com.mixcus.server.dto.UserDto;
import com.mixcus.server.dto.UserResultDto;
import com.mixcus.server.service.UserService;
import com.mixcus.server.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public String getPassword(String userName) {
        return userDao.getPassword(userName);
    }

    @Override
    public int hasUserName(String userName) {
        return userDao.hasUserName(userName);
    }

    @Override
    public Character getUserRole(String userName) {
        return userDao.getUserRole(userName);
    }

    @Override
    public void addUser(UserDto userDto) {
        userDao.addUser(userDto);
    }

    @Override
    public String getToken(UserDto userDto) {

        Map<String,Object> claims = new HashMap<>();

        claims.put("userName",userDto.getUserName());
        claims.put("password",userDto.getUserPassword());


        return JWTUtil.getToken(claims);
    }

    @Override
    public UserResultDto getUserResult(UserDto userDto) {

        UserResultDto res = new UserResultDto();
        res.setUserName(userDto.getUserName());
        res.setUserRole(userDto.getUserRole());
        res.setToken(getToken(userDto));

        return res;
    }
}